package srm515;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class RotatedClock {
    public String getEarliest(int hourHand, int minuteHand) {
        Set<String> set = new HashSet<String>();
        for (int i = 0; i < 12; i++) {
            int rot = i * 30;
            for (int j = 0; j < 12; j++) {
                int m = (minuteHand + i * 30 + j * 30) % 360;
                int h = (hourHand + i * 30 + j * 30) % 360;
                int d = h % 30;
                int nm = (360 * d) / 30;
                if (nm == m) {
                    set.add(String.format("%02d:%02d", h / 30, m / 6));
                }
            }
        }
        if (set.size() == 0) {
            return "";
        }
        String vals[] = set.toArray(new String[]{});
        Arrays.sort(vals);
        return vals[0];
    }

    // BEGIN KAWIGIEDIT TESTING
    // Generated by KawigiEdit 2.1.4 (beta) modified by pivanof
    private static boolean KawigiEdit_RunTest(int testNum, int p0, int p1, boolean hasAnswer, String p2) {
        System.out.print("Test " + testNum + ": [" + p0 + "," + p1);
        System.out.println("]");
        RotatedClock obj;
        String answer;
        obj = new RotatedClock();
        long startTime = System.currentTimeMillis();
        answer = obj.getEarliest(p0, p1);
        long endTime = System.currentTimeMillis();
        boolean res;
        res = true;
        System.out.println("Time: " + (endTime - startTime) / 1000.0 + " seconds");
        if (hasAnswer) {
            System.out.println("Desired answer:");
            System.out.println("\t" + "\"" + p2 + "\"");
        }
        System.out.println("Your answer:");
        System.out.println("\t" + "\"" + answer + "\"");
        if (hasAnswer) {
            res = answer.equals(p2);
        }
        if (!res) {
            System.out.println("DOESN'T MATCH!!!!");
        } else if ((endTime - startTime) / 1000.0 >= 2) {
            System.out.println("FAIL the timeout");
            res = false;
        } else if (hasAnswer) {
            System.out.println("Match :-)");
        } else {
            System.out.println("OK, but is it right?");
        }
        System.out.println("");
        return res;
    }

    public static void main(String[] args) {
        boolean all_right;
        all_right = true;

        int p0;
        int p1;
        String p2;

        // ----- test 0 -----
        p0 = 70;
        p1 = 300;
        p2 = "08:20";
        all_right = KawigiEdit_RunTest(0, p0, p1, true, p2) && all_right;
        // ------------------

        // ----- test 1 -----
        p0 = 90;
        p1 = 120;
        p2 = "11:00";
        all_right = KawigiEdit_RunTest(1, p0, p1, true, p2) && all_right;
        // ------------------

        // ----- test 2 -----
        p0 = 240;
        p1 = 36;
        p2 = "";
        all_right = KawigiEdit_RunTest(2, p0, p1, true, p2) && all_right;
        // ------------------

        // ----- test 3 -----
        p0 = 19;
        p1 = 19;
        p2 = "";
        all_right = KawigiEdit_RunTest(3, p0, p1, true, p2) && all_right;
        // ------------------

        // ----- test 4 -----
        p0 = 1;
        p1 = 12;
        p2 = "00:02";
        all_right = KawigiEdit_RunTest(4, p0, p1, true, p2) && all_right;
        // ------------------

        if (all_right) {
            System.out.println("You're a stud (at least on the example cases)!");
        } else {
            System.out.println("Some of the test cases had errors.");
        }
    }
    // END KAWIGIEDIT TESTING
}
//Powered by KawigiEdit 2.1.4 (beta) modified by pivanof!
